package model

import (
	"context"
	"fmt"
	"github.com/qiniu/go-sdk/v7/auth/qbox"
	"github.com/qiniu/go-sdk/v7/storage"
	"mime/multipart"
	"next-vue-backend/utils"
	"next-vue-backend/utils/errmsg"
)

var (
	AccessKey  = utils.AccessKey
	SecretKey = utils.SecretKey
	Bucket = utils.Bucket
	ImgUrl = utils.QiniuServer
)

// UploadFile 七牛上传
func UploadFile (file multipart.File,filesize int64) (string, int) {
	putPolicy := storage.PutPolicy{
		Scope: Bucket,
	}

	mac := qbox.NewMac(AccessKey,SecretKey)
	upToken := putPolicy.UploadToken(mac)
	cfg := storage.Config{
		Zone: &storage.ZoneHuanan,
		UseCdnDomains: false,
		UseHTTPS: false,

	}
	putExtra := storage.PutExtra{}

	formUploader := storage.NewFormUploader(&cfg)
	ret := storage.PutRet{}
	err := formUploader.PutWithoutKey(context.Background(), &ret, upToken, file, filesize, &putExtra)
	if err != nil {
		fmt.Println(err)
		return  "", errmsg.ERROR
	}
	url := ImgUrl + ret.Key
	return url, errmsg.SSUCCESS
}

